package com.situ.company.department.service;

import java.util.List;

import com.situ.company.department.dao.DepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.employee.dao.EmployeeDao;
import com.situ.company.employee.model.EmployeeModel;

//业务层--业务逻辑
public class DepartmentService {

    private DepartmentDao dao         = new DepartmentDao();
    private EmployeeDao   employeeDao = new EmployeeDao();

    public String insert(DepartmentModel model) {
        if (selectModel(model) == null) {
            Integer res = dao.insert(model);
            return res == null ? null : res.toString();
        } else
            return "repeat";
    }

    public String delete(DepartmentModel model) {
        EmployeeModel m1 = new EmployeeModel();
        m1.setCodeDept(model.getCode());
        List<EmployeeModel> list = employeeDao.select(m1);
        if (list == null || list.isEmpty()) {
            Integer res = dao.delete(model);
            return res == null ? null : res.toString();
        } else
            return "exist";
    }

    public String update(DepartmentModel model) {
        Integer res = dao.update(model);
        return res == null ? null : res.toString();
    }

    public DepartmentModel selectModel(DepartmentModel model) {
        DepartmentModel m0 = new DepartmentModel(model.getCode());
        List<DepartmentModel> list = dao.select(m0);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    public List<DepartmentModel> selectList(DepartmentModel model) {
        String code = model.getCode();
        if (code == null)
            code = "";
        model.setCode("%" + code + "%");
        String name = model.getName();
        if (name == null)
            name = "";
        model.setName("%" + name + "%");
        return dao.select(model);
    }

    public Integer selectCount(DepartmentModel model) {
        DepartmentModel m0 = new DepartmentModel();
        String code = model.getCode();
        if (code == null)
            code = "";
        m0.setCode("%" + code + "%");
        String name = model.getName();
        if (name == null)
            name = "";
        m0.setName("%" + name + "%");
        return dao.selectCount(m0);
    }

}
